/**
 * 
 */
package com.wqr.it.ecwdr.client.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.util.URIUtil;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;


/**
 * @author adinos
 *
 */
public class WebClientUtil {
	private static final org.slf4j.Logger log = LoggerFactory.getLogger("INFO");
	protected static String accept = "image/jpeg,text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
	protected static String charset = "UTF-8";
	protected static String contentType = "text/html; charset=" + charset;
	protected static final Random ran = new Random();
    

	
	public static String doGet(String url, String queryString) {
		String html = null;
		int i = 0;
		do {
			html = get(url, queryString);
			i++;
		} while ((html == null || html.indexOf("Robot Check") != -1) && i < 5);
		return html;
	}
	
	protected static String get(String url, String queryString) {
		String str;
		String html = null;
		
		HttpMethod httpMethod = createGetMethod(url);
		BufferedReader in = null;
        try {
        	HttpClient client = new HttpClient(); 
			
			client.getHttpConnectionManager().getParams().setDefaultMaxConnectionsPerHost(256);
			client.getHttpConnectionManager().getParams().setMaxTotalConnections(256);
			client.getHttpConnectionManager().getParams().setConnectionTimeout(300000); 
			client.getHttpConnectionManager().getParams().setSoTimeout(300000);
			client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,charset);
			
            if (!StringUtils.isEmpty(queryString))  {
            	httpMethod.setQueryString(URIUtil.encodeQuery(queryString)); 
            }
            
            client.executeMethod(httpMethod);
			
			in = new BufferedReader(new InputStreamReader(httpMethod.getResponseBodyAsStream(), charset)); 
            StringBuffer sb = new StringBuffer();
            do {
            	str = in.readLine();
            	if (str != null && !str.equals("\n")) {
            		sb.append(str + "\n");
            	}
            } while (str != null);
            html = sb.toString();
            client = null;
            if (html.indexOf("Robot Check") != -1
            		|| html.indexOf("Maximum number of open connections reached") != -1) {

				log.debug("Robot Check");
            }
		} catch (HttpException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			log.error(e.getMessage());
			e.printStackTrace();
			try {
				Thread.sleep(60000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		} finally {
			if (in != null) {
				try {
					in.close();
					in = null;
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
			if (httpMethod != null) {
				httpMethod.releaseConnection();
				httpMethod = null;
			}
		}
		return html;
	}
	
	public static String doPost(String url, Map<String, String> params) {
		NameValuePair [] nameValuePairArray = null;
		if (params != null) { 
			List<NameValuePair> list = new ArrayList<NameValuePair>();
        	for (Map.Entry<String, String> entry : params.entrySet()) { 
        		 list.add(new NameValuePair(entry.getKey(), entry.getValue()));
        	}
        	nameValuePairArray = list.toArray(new NameValuePair [list.size()]);
        } 
		return doPost(url, nameValuePairArray);
	}

	
	public static String doPost(String url, NameValuePair [] params) {
		String html = null;
		int i = 0;
		do {
			html = post(url, params);
			i++;
			if (html.indexOf("Robot Check") != -1) {
				html = null;
			}
			if (html == null) {
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		} while (html == null && i < 25);
		return html;
	}
	
	protected static String post(String url, NameValuePair [] params) {
		String str;
		String html = null;
		
		PostMethod httpMethod = createPostMethod(url);
		HttpClient client = null;
		BufferedReader in = null;
        try {
            if (params != null) { 
				httpMethod.setRequestBody(params);
            } 
        	client = new HttpClient(); 
			client.getHttpConnectionManager().getParams().setConnectionTimeout(300000); 
			client.getHttpConnectionManager().getParams().setSoTimeout(300000);
			client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,charset);
			
			client.executeMethod(httpMethod);
			
			in = new BufferedReader(new InputStreamReader(httpMethod.getResponseBodyAsStream(), charset)); 
            StringBuffer sb = new StringBuffer();
            do {
            	str = in.readLine();
            	if (str != null) {
            		sb.append(str + "\n");
            	}
            } while (str != null);
            html = sb.toString();
            if (html.indexOf("Robot Check") != -1
            		|| html.indexOf("Maximum number of open connections reached") != -1) {
            	html = null;
            }
		} catch (HttpException e) {
			log.error(e.getMessage());
			//e.printStackTrace();
		} catch (IOException e) {
			log.error(e.getMessage());
			//e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
					in = null;
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
        client = null;
        if (httpMethod != null) {
        	httpMethod.releaseConnection();
        	httpMethod = null;
        }
		return html;
	}
	
	protected static HttpMethod createGetMethod(String url) {
		//String ip = HttpTookit.generateIp();
		HttpMethod method = new GetMethod(url); 
        
        method.setRequestHeader("Content-Type", contentType);
        method.setRequestHeader("User-Agent", UserAgentUtil.getAgent());
        //method.setRequestHeader("Cookie", cookie);
        method.setRequestHeader("Accept", accept);
        method.setRequestHeader("Connection", "close");
        return method;
	}
	
	protected static PostMethod createPostMethod(String url) {
		//String ip = HttpTookit.generateIp();
		PostMethod method = new PostMethod(url); 
        
        method.setRequestHeader("Content-Type", contentType);
        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        method.setRequestHeader("User-Agent", UserAgentUtil.getAgent());
        //method.setRequestHeader("Cookie", cookie);
        method.setRequestHeader("Accept", accept);
        method.setRequestHeader("Connection", "close");
        return method;
	}
	
	
}
